package com.lavegue.webaction;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.lavegue.common.BaseAction;
import com.lavegue.common.Constants;
import com.lavegue.common.ListAction;
import com.lavegue.common.Page;
import com.lavegue.model.BisGroup;
import com.lavegue.model.Company;
import com.lavegue.model.EveryDayConsumptionAggregate;
import com.lavegue.model.Msg;
import com.lavegue.model.TopUp;
import com.lavegue.model.UserInfo;
import com.lavegue.service.MsgService;
import com.lavegue.service.TopUpService;
import com.lavegue.service.UserInfoService;
import com.lavegue.util.StringUtil;

public class AccountManagerAction extends ListAction<Msg>{
	private UserInfo user;
	private Msg msg;
	private UserInfoService userInfoService;
	private MsgService msgService;
	private TopUpService topUpService;
	
	private String ActionError; // 界面显示的错误信息
	private String beginTime;   //消费明细-->选择开始时间
	private String endTime;     //消费明细-->选择结束时间
	private Page<Msg> pageMsgObj; //用户消息分页对象
	private Page<TopUp> pageTopUpObj; //用户充值记录分页对象
	private Page<EveryDayConsumptionAggregate> pageEcaObj; //每日消费汇总的类
	private TopUp topUp;
	private int topupId;
	
	public Page<EveryDayConsumptionAggregate> getPageEcaObj() {
		return pageEcaObj;
	}

	public void setPageEcaObj(Page<EveryDayConsumptionAggregate> pageEcaObj) {
		this.pageEcaObj = pageEcaObj;
	}

	public Page<TopUp> getPageTopUpObj() {
		return pageTopUpObj;
	}

	public void setPageTopUpObj(Page<TopUp> pageTopUpObj) {
		this.pageTopUpObj = pageTopUpObj;
	}

	public Page<Msg> getPageMsgObj() {
		return pageMsgObj;
	}

	public void setPageMsgObj(Page<Msg> pageMsgObj) {
		this.pageMsgObj = pageMsgObj;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public TopUpService getTopUpService() {
		return topUpService;
	}

	public void setTopUpService(TopUpService topUpService) {
		this.topUpService = topUpService;
	}

	public MsgService getMsgService() {
		return msgService;
	}

	public void setMsgService(MsgService msgService) {
		this.msgService = msgService;
	}

	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo user) {
		this.user = user;
	}
	public Msg getMsg() {
		return msg;
	}

	public void setMsg(Msg msg) {
		this.msg = msg;
	}
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public String getActionError() {
		return ActionError;
	}

	public void setActionError(String actionError) {
		ActionError = actionError;
	}

	/**20111226
	 * 我的易彩通 > 我的帐户 > 帐户管理（myAccount.jsp）
	 * @return
	 */
	public String doMyAccount(){
		if (this.getSessionUser() == null) {
			return G_LOGIN_PAGE;
		}
//		UserInfo userInfo = userInfoService.testUserByMobile(); //假数据测试
//		setSessionAtrribute(Constants.SESSION_USER,userInfo);
		// 判断用户信息是否存在
		UserInfo  user = this.getSessionUser();

		Integer uid = user.getId(); //获取用户ID
		try {
			List<Msg> listMsg = msgService.findMsgByUid(uid);
			if(null != listMsg && !listMsg.isEmpty()){
				msg = (Msg)listMsg.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "myAccountPage";
	}
	
	//进入[消费明细页面]加载数据
	public String doConsumptionDetailList() throws Exception{
//		UserInfo userInfo = userInfoService.testUserByMobile(); //假数据测试
//		setSessionAtrribute(Constants.SESSION_USER,userInfo);
		// 判断用户信息是否存在
		UserInfo  user= this.getSessionUser();
		
		Integer senderUid = user.getId();
		/**ljp 20111227
		 * 查询发送者用户的所有彩信信息
		 * 第一次加载传入的条件值为空
		 */
		pageMsgObj = msgService.findMsgList(pageNo, pageSize,null,null,null,senderUid);
				
		return "consumptionDetailPage";
	}
	
	//[消费明细页面]-->查看按钮钓鱼
	public String doSerachMsgList() throws Exception{
		if (null == this.getSessionUser()) {
			return G_LOGIN_PAGE;
		}
//		UserInfo userInfo = userInfoService.testUserByMobile(); //假数据测试
//		setSessionAtrribute(Constants.SESSION_USER,userInfo);
		// 判断用户信息是否存在
		UserInfo  user= this.getSessionUser();
		
		Integer senderUid = user.getId();
		String titleName = String.valueOf("");
		if(null != msg){
			titleName = msg.getTitle();
		}
		
		//将String转换为Date类型
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
		Date beginDate = null;
		Date endDate = null;
		
		if(null != beginTime && !beginTime.equals("")){
			beginDate = sdf.parse(beginTime);

		}
		if(null != endTime && !endTime.equals("")){
			endDate = sdf.parse(endTime); 
		}
		/**ljp 20111227
		 * 查询发送者用户的所有彩信信息
		 */
		pageMsgObj = msgService.findMsgList(pageNo, pageSize,titleName,beginDate,endDate,senderUid.intValue());
				
		return "consumptionDetailPage";
	}
	
	//进入[每日消费汇总]入口加载数据
	public String toLoadEveryDayConsumption() throws Exception{
		if (null == this.getSessionUser()) {
			return G_LOGIN_PAGE;
		}
//		UserInfo userInfo = userInfoService.testUserByMobile(); //假数据测试
//		setSessionAtrribute(Constants.SESSION_USER,userInfo);
		// 判断用户信息是否存在
		UserInfo  user= this.getSessionUser();
		
		Integer senderUid = user.getId();		
		/**ljp 20111228
		 * 查询发送者用户的 每日消费汇总 信息
		 * 第一次加载传入的条件值为空pageMsgObj
		 */
		 pageEcaObj= msgService.findEveryDayConsumptionList(pageNo, pageSize,null,senderUid);
		 BigDecimal dangtianMoney = msgService.getDtConsumptionMoney(senderUid);
		 this.setRequestAtrribute("dangtianMoney", dangtianMoney);
		 
		return "everyDayConsumptionPage";
	}
	
	//进入[每日消费汇总]-->查看按钮调用
	public String doEveryDayConsumptionList() throws Exception{
		if (null == this.getSessionUser()) {
			return G_LOGIN_PAGE;
		}
//		UserInfo userInfo = userInfoService.testUserByMobile(); //假数据测试
//		setSessionAtrribute(Constants.SESSION_USER,userInfo);
		// 判断用户信息是否存在
		UserInfo  user= this.getSessionUser();
		
		Integer senderUid = user.getId();
		//将String转换为Date类型
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
		Date dateTime = null;
		if(null != beginTime && !beginTime.equals("")){
			dateTime = sdf.parse(beginTime);

		}
		
		/**ljp 20111228
		 * 查询发送者用户的 每日消费汇总 信息pageMsgObj
		 */
		 pageEcaObj= msgService.findEveryDayConsumptionList(pageNo, pageSize,dateTime,senderUid);
		 BigDecimal dangtianMoney = msgService.getDtConsumptionMoney(senderUid);
		 this.setRequestAtrribute("dangtianMoney", dangtianMoney);
		 
		return "everyDayConsumptionPage";
	}
	
	//进入[充值记录页面]加载数据
	public String toLoadRechargeRecordList() throws Exception{
		if (null == this.getSessionUser()) {
			return G_LOGIN_PAGE;
		}
//		UserInfo userInfo = userInfoService.testUserByMobile(); //假数据测试
//		setSessionAtrribute(Constants.SESSION_USER,userInfo);
		// 判断用户信息是否存在
		UserInfo  user= this.getSessionUser();
		
		Integer senderUid = user.getId();
		/**ljp 20111229
		 * 查询发送者用户的所有彩信信息
		 * 第一次加载传入的条件值为空
		 */
		pageTopUpObj = topUpService.findMsgRechargeRecordList(pageNo, pageSize,null,null,senderUid.intValue());
				
		return "topUpRecordPage";
	}
	
	public String doRechargeRecordList() throws Exception{
		if (this.getSessionAtrribute(Constants.MAN_SESSION_USER) == null) {
			return "lg";
		}
//		UserInfo userInfo = userInfoService.testUserByMobile(); //假数据测试
//		setSessionAtrribute(Constants.SESSION_USER,userInfo);
		// 判断用户信息是否存在
		UserInfo  user= this.getSessionUser();
		
		Integer senderUid = user.getId();
		
		//将String转换为Date类型
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
		Date beginDate = null;
		Date endDate = null;
		
		if(null != beginTime && !beginTime.equals("")){
			beginDate = sdf.parse(beginTime);

		}
		if(null != endTime && !endTime.equals("")){
			endDate = sdf.parse(endTime); 
		}
		
		/**ljp 20111229
		 * 查询发送者用户的所有彩信信息
		 */
		pageTopUpObj = topUpService.findMsgRechargeRecordList(pageNo, pageSize,beginDate,endDate,senderUid.intValue());
		
		
		return "topUpRecordPage";
	}
	
	
	/**
	 * 新的消费明细功能
	 * @return
	 * @throws Exception 
	 */
	public String xiaofeimingxi() throws Exception {
			if (null == this.getSessionUser()) {
				return G_LOGIN_PAGE;
			}
//			UserInfo userInfo = userInfoService.testUserByMobile(); //假数据测试
//			setSessionAtrribute(Constants.SESSION_USER,userInfo);
			// 判断用户信息是否存在
			UserInfo  user= this.getSessionUser();
			//将String转换为Date类型
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
			Date beginDate = null;
			Date endDate = null;
			
			if(null != beginTime && !beginTime.equals("")){
				beginDate = sdf.parse(beginTime);

			}
			if(null != endTime && !endTime.equals("")){
				endDate = sdf.parse(endTime); 
			}
			int senderUid = user.getId();
			/**ljp 20111229
			 * 查询发送者用户的所有彩信信息
			 * 第一次加载传入的条件值为空
			 */
			pageTopUpObj = topUpService.findMsgRechargeRecordList(pageNo, pageSize,beginDate,endDate,senderUid);
			return "xfmxPage";
	}
	
	public String showTopupDetail() {
		if (null == this.getSessionUser()) {
			return G_LOGIN_PAGE;
		}
		topUp = topUpService.findById(topupId);
		return "topupDetail";
	}
	
	public String showXiaofeiDetail() {
		if (null == this.getSessionUser()) {
			return G_LOGIN_PAGE;
		}
		this.pageMsgObj = msgService.findMsgByTopUp(pageNo, pageSize, topupId);
		return "xiaofeiDetail";
	}
	
	public TopUp getTopUp() {
		return topUp;
	}

	public void setTopUp(TopUp topUp) {
		this.topUp = topUp;
	}

	public int getTopupId() {
		return topupId;
	}

	public void setTopupId(int topupId) {
		this.topupId = topupId;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		

	}

}
